/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-4-3
 */
package persist;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import code.extract.CEConfig;
import code.extract.FileDealer;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Durability;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;

public class GooglePageDB {
	private Environment env;
	private Database db;
	private long cacheSize = 1024 * 1024;
	private String DBName = null;
	private String encode = null;

	public static void main(String[] args) {
		String testDirName = CEConfig.getGoogleBDBDirname();
//		String testDirName = "testgooglefl";
		File testDir = new File(testDirName);
		if (!testDir.exists()){
			testDir.mkdirs();
		}
		GooglePageDB testDB = new GooglePageDB(testDirName, 1024*1024, "utf8");
		testDB.open("googlebdb");
		String value;
		try {
//			value = FileDealer.getFileContent("C:/Users/ftc/Desktop/代码块提取/test_page/8_javaeye.txt");
//			value = "lulu123555";
			String key = "com.sun.corba.se.PortableActivationIDL.Activator^1";
			try{
//				testDB.put(key, value);
				System.out.println(testDB.get(key));
//				testDB.put("fangfang", "lulu");
				System.out.println(testDB.get("fangfang"));
//				System.out.println(testDB.get("123"));
			}catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		testDB.close();
	}

	public GooglePageDB(String path, long cacheSize, String encode) {
		EnvironmentConfig envConfig = new EnvironmentConfig();
		envConfig.setAllowCreate(true);
		envConfig.setCacheSize(cacheSize);
		envConfig.setTransactional(true);
		envConfig.setDurability(Durability.COMMIT_SYNC);    
		
		try {
			env = new Environment(new File(path), envConfig);
			this.setCacheSize(cacheSize);
			if (encode == null) {
				Properties initProp = new Properties(System.getProperties());
				this.encode = initProp.getProperty("file.encoding");
			} else {
				this.encode = encode;
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
	}
	public void printAllDatabase(){
		   java.util.List<String> myDbNames = env.getDatabaseNames(); 
           for(int i=0; i < myDbNames.size(); i++) {
               System.out.println("Database Name: " + (String)myDbNames.get(i));
           }
	}
	public void open(String dbName) {
		DatabaseConfig dbConfig = new DatabaseConfig();
		dbConfig.setAllowCreate(true);
		dbConfig.setTransactional(true);
		
		try {
			db = env.openDatabase(null, dbName, dbConfig);
			setDBName(dbName);
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
	}
	
	public void sync() {
		db.sync();
	}

	public void close() {
		try {
			if (db != null) {
				db.close();
			}
			if (env != null) {
				env.cleanLog();
				env.close();
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param key(should be a class name or other keywords)
	 * @return
	 * @throws Exception
	 */
	public String get(String key) throws Exception {
		DatabaseEntry queryKey = new DatabaseEntry();
		DatabaseEntry value = new DatabaseEntry();
		queryKey.setData(key.getBytes(encode));
		OperationStatus status = db
				.get(null, queryKey, value, LockMode.DEFAULT);
		if (status == OperationStatus.SUCCESS) {
			return new String(value.getData());
		}
		return null;
	}
	/**
	 * @param key(should be a class name or other keywords)
	 * @param value(should be a google result page)
	 * @return
	 * @throws Exception
	 */
	public boolean put(String key, String value) throws Exception {
		byte[] theKey = key.getBytes(encode);
		byte[] theValue = value.getBytes(encode);
		Transaction txn = env.beginTransaction(null, null);
		OperationStatus status = db.put(txn, new DatabaseEntry(theKey),
				new DatabaseEntry(theValue));
		txn.commit();
		if (status == OperationStatus.SUCCESS) {
			return true;
		}
		return false;
	}

	public void setCacheSize(long cacheSize) {
		this.cacheSize = cacheSize;
	}

	public long getCacheSize() {
		return cacheSize;
	}

	public void setDBName(String dBName) {
		DBName = dBName;
	}

	public String getDBName() {
		return DBName;
	}
}
